package com.yanchangapp.ui.presenter;

import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.request.UpRecordRequest;
import com.yanchangapp.model.response.RecordDetailResponse;
import com.yanchangapp.model.response.UpdateRecordResponse;
import com.yanchangapp.ui.view.RecordRejectAtView;
import com.zxl.baselib.bean.response.BaseResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019-08-23.
 * description: 采购记录详情P层
 */
public class RecordRejectAtPresenter extends BasePresenter<RecordRejectAtView> {

    public RecordRejectAtPresenter(BaseActivity context) {
        super(context);
    }

    public void queryRecordDetail(int itemID){
        HeadApiRetrofit.getInstance().queryRecordDetailByItemId(itemID)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<RecordDetailResponse>>() {
                    @Override
                    public void onNext(BaseResponse<RecordDetailResponse> recordDetailResponseBaseResponse) {
                        if (null != getView()){
                            if (AppConstant.REQUEST_SUCCESS == recordDetailResponseBaseResponse.getCode()){
                                getView().queryDetailSuccess(recordDetailResponseBaseResponse.getData());
                            }else {
                                getView().queryDetailFailure(recordDetailResponseBaseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (null != getView()){
                            getView().queryDetailFailure(exception.getErrorInfo());
                        }
                    }
                });
    }

    public void submitRecord(int id,String supplierId,int purchaseNum){
        if (null != getView()){
            getView().showLoadingDialog();
        }
        UpRecordRequest request = new UpRecordRequest(id,supplierId,purchaseNum);
        HeadApiRetrofit.getInstance().updateRecordProduct(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<UpdateRecordResponse>() {
                    @Override
                    public void onNext(UpdateRecordResponse baseResponse) {
                        if (null != getView()){
                            getView().hideLoadingDialog();
                            if (AppConstant.REQUEST_SUCCESS == baseResponse.getCode()){
                                getView().editRecordSuccess();
                            }else {
                                getView().editRecordFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (null != getView()){
                            getView().hideLoadingDialog();
                            getView().editRecordFailure(exception.getErrorInfo());
                        }
                    }
                });
    }

    public void submitToApprove(int id){
        if (null != getView()){
            getView().showLoadingDialog();
        }
        HeadApiRetrofit.getInstance().submitRecordByID(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (null != getView()){
                            getView().hideLoadingDialog();
                            if (AppConstant.REQUEST_SUCCESS == baseResponse.getCode()){
                                getView().submitToApproveSuccess();
                            }else {
                                getView().submitToApproveFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (null != getView()){
                            getView().hideLoadingDialog();
                            getView().submitToApproveFailure(exception.getErrorInfo());
                        }
                    }
                });
    }

    public void deleteApprove(int id){
        if (null != getView()){
            getView().showLoadingDialog();
        }
        HeadApiRetrofit.getInstance().deleteRecordByID(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (null != getView()){
                            getView().hideLoadingDialog();
                            if (AppConstant.REQUEST_SUCCESS == baseResponse.getCode()){
                                getView().deleteSuccess();
                            }else {
                                getView().deleteFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (null != getView()){
                            getView().hideLoadingDialog();
                            getView().deleteFailure(exception.getErrorInfo());
                        }
                    }
                });
    }
}
